<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" type="info" size="small" round icon="search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <ArticleList :channel="item"></ArticleList>
      </van-tab>

      <!-- 更多 -->
      <div slot="nav-right" class="hamburger-btn" @click="isEditChannelShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- popup 弹出框 -->
    <van-popup
      class="edit-channel-popup"
      v-model="isEditChannelShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <ChannelEdit
        @update-active="onUpdateActive"
        :active="active"
        :channels="channels"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from '@/views/home/components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      active: 0,
      // 计算属性的时候 , data 里面的数据发生变化, 当前组件内的指令 和 表达式就会重新计算
      // skill  => {{ skill }}
      // age => {{  age }}
      channels: [], // tab栏要展示的标题
      isEditChannelShow: false, // 控制pop 弹出框的显示
    }
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
  async created() {
    // let res = await getUserChannels()
    // console.log('用户频道', res.data)
    // this.channels = res.data.data.channels

    // 登录
    if (this.user.token) {
      console.log('1')
      let res = await getUserChannels()
      console.log('用户频道', res.data)
      this.channels = res.data.data.channels
    }
    // 未登录
    else {
      //1. 尝试从本地获取
      let localChannels = getItem('TOUTIAO_CHANNELS')

      //2.  本地有值, 就直接用
      if (localChannels) {
        console.log('2.1')
        this.channels = localChannels
      } else {
        console.log('2.2')
        //3. 本地没有值, 发送请求 (无 token )
        let res = await getUserChannels()
        console.log('用户频道', res.data)
        this.channels = res.data.data.channels
      }
    }

    //登录
    // if (this.user.token) {
    //   // TODO:
    //   // 发送请求 (有 token)
    //   let res = await getUserChannels()
    //   console.log('用户频道', res.data)
    //   this.channels = res.data.data.channels
    // }
    // //未登录
    // else {
    //   //1. 尝试从本地获取
    //   let localChannels = getItem('TOUTIAO_CHANNELS')
    //   //2. 本地有值, 就直接用
    //   if (localChannels) {
    //     this.channels = localChannels
    //   } else {
    //     //3. 本地没有值, 发送请求 (无 token )
    //     let res = await getUserChannels()
    //     console.log('用户频道', res.data)
    //     this.channels = res.data.data.channels
    //   }
    // }
  },
  mounted() {},
  methods: {
    // 切换频道
    onUpdateActive(index, isEditShow = false) {
      this.active = index // 1
      this.isEditChannelShow = isEditShow
    },
  },
}
</script>

<style scoped lang="less">
.home-container {
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}

/deep/ .channel-tabs {
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 80px;
    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }

  .van-tab--active .van-tab__text {
    color: #333 !important;
  }
  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }
  .van-tabs__nav {
    padding: 0;
  }
}

.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.toutiao {
    font-size: 33px;
  }
  &:before {
    content: '';
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    // background-image: url(~@/assets/gradient-gray-line.png);
    background: red;
    background-size: contain;
  }
}

.home-container {
  padding-top: 180px;
  padding-bottom: 100px;
}

/deep/ .van-tabs__wrap {
  position: fixed;
  top: 92px;
  z-index: 2;
  width: 100%;
}

.edit-channel-popup {
  padding-top: 100px;
  box-sizing: border-box;
}
</style>
